package com.qf.tomcat;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.HashMap;

public class HttpRequestAndResponse {

    private HashMap<String,HttpServlet> servletHashMap;

    public HttpRequestAndResponse(HashMap<String,HttpServlet> servletHashMap){
        this.servletHashMap =servletHashMap;
    }

    public void httpAccepted(Socket socket) throws IOException{
        OutputStream outputStream =socket.getOutputStream();
        String begin = "HTTP/1.1 202 Accepted\n" +
                // "Location: http://www.baidu.com\n"+
                "Date: Mon, 27 Jul 2009 12:28:53 GMT\n" +
                "Server: Apache\n";
        outputStream.write(begin.getBytes());
        outputStream.flush();
    }

    public void request(Socket socket) throws IOException{
        InputStream inputStream =socket.getInputStream();
        byte[] buffer = new byte[1024];
        int len =0;
        StringBuilder stringBuilder = new StringBuilder();
        while (true){
            len=inputStream.read(buffer);
//            if (len == -1){
//                break;
//            }

            System.out.println(new String(buffer,0,len));
            stringBuilder.append(new String(buffer,0,len));

            if (len<1024){
                break;
            }
        }
             HttpProtocol httpProtocol =parseHttpStr(stringBuilder.toString());
             String servletUri = httpProtocol.getServletUri();
             HttpServlet httpServlet =servletHashMap.get(servletUri);
             if ("Get".toUpperCase().equals(httpProtocol.getRequestMethod())){
                 if (httpServlet != null){
                     httpServlet.doGet();
                 }
                }else if ("Post".toUpperCase().equals(httpProtocol.getRequestMethod())){
                    if (httpServlet != null){
                        httpServlet.doPost();
                    }
             }
//                    inputStream.close();
               }


        public void response(Socket socket) throws IOException{
            OutputStream outputStream = socket.getOutputStream();
            String body = "<html><head></head><body><h1>HelloWorld</h1></body></html>";

            String str = "HTTP/1.1 200 OK\n" +
                    // "Location: http://www.baidu.com\n"+
                    "Date: Mon, 27 Jul 2009 12:28:53 GMT\n" +
                    "Server: Apache\n" +
                    "Last-Modified: Wed, 22 Jul 2009 19:15:56 GMT\n" +
                    "ETag: \"34aa387-d-1568eb00\"\n" +
                    "Accept-Ranges: bytes\n" +
                    "Content-Length: "+body.length()+"\n" +
                    "Vary: Accept-Encoding\n" +
                    "Content-Type: text/html\n"+
                    "\n"+body;
            outputStream.write(str.getBytes());
            outputStream.flush();
//
//              outputStream.close();
        }


        private HttpProtocol parseHttpStr(String http){
        HttpProtocol httpProtocol = new HttpProtocol();
        String[] strings = http.split("\n");
            System.out.println(strings);
            for (int i = 0,len=strings.length; i < len; i++){
                if (i == 0){
                    String[] stateLinesParam = strings[0].split(" ");
                    httpProtocol.setRequestMethod(stateLinesParam[0]);
                    httpProtocol.setUrl(stateLinesParam[1]);
                    String[] split = stateLinesParam[1].split("\\?");
                    httpProtocol.setServletUri(split[0]);
                }
            }

            return httpProtocol;
        }
}
